// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

// 引入API工具
const api = require('../../utils/api.js')

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    banners: [
      {
        id: 1,
        imageUrl: '/assets/images/banner1.jpg'
      },
      {
        id: 2,
        imageUrl: '/assets/images/banner2.jpg'
      },
      {
        id: 3,
        imageUrl: '/assets/images/banner3.jpg'
      }
    ],
    bannersLoading: false,
    notices: [
      '请爱护装备，使用过程中注意保护',
      '借用前请仔细检查装备是否完好',
      '按时归还，逾期将影响信用分',
      '如有损坏，请及时联系管理员'
    ],
    hotEquipments: [],
    loading: false
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  onLoad() {
    this.loadHotEquipments();
  },
  // 加载热门装备
  loadHotEquipments() {
    this.setData({
      loading: true
    });
    
    // 临时使用静态数据，后续可以替换为云函数调用
    setTimeout(() => {
      this.setData({
        hotEquipments: [
          {
            _id: 'equip001',
            name: '户外帐篷',
            imageUrl: '/assets/images/帐篷.jpg',
            status: '可借用'
          },
          {
            _id: 'equip002',
            name: '登山背包',
            imageUrl: '/assets/images/登山背包.jpg',
            status: '可借用'
          },
          {
            _id: 'equip003',
            name: '睡袋',
            imageUrl: '/assets/images/睡袋.jpg',
            status: '已借出'
          },
          {
            _id: 'equip004',
            name: '登山杖',
            imageUrl: '/assets/images/登山杖.jpg',
            status: '可借用'
          }
        ],
        loading: false
      });
    }, 500);
    
    // 注释掉云函数调用，等后续需要时再实现
    /*
    wx.cloud.callFunction({
      name: 'getHotEquipments',
      data: {
        limit: 4
      },
      success: res => {
        this.setData({
          hotEquipments: res.result.data || [],
          loading: false
        });
      },
      fail: err => {
        console.error('[云函数] [getHotEquipments] 调用失败', err);
        this.setData({
          loading: false
        });
      }
    });
    */
  },
  onSearchInput(e) {
    const keyword = e.detail.value;
    // 实现搜索逻辑
    wx.navigateTo({
      url: `/pages/equipment/equipment?keyword=${keyword}`
    });
  },
  onBannerTap(e) {
    const { id } = e.currentTarget.dataset;
    // 处理轮播图点击事件
    wx.showToast({
      title: '查看详情',
      icon: 'none'
    });
  },
  onEquipmentTap(e) {
    const { id } = e.currentTarget.dataset;
    // 处理装备点击事件
    wx.navigateTo({
      url: `/pages/equipment/detail?id=${id}`
    });
  },
  navigateToEquipment() {
    wx.switchTab({
      url: '/pages/equipment/equipment'
    });
  },
  navigateToProfile() {
    wx.switchTab({
      url: '/pages/profile/profile'
    });
  }
})
